/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.qb9.flashlib.tasks
{
	import com.qb9.flashlib.utils.ArrayUtil;

	/** Abstract base class for all task collection classes. */
	public class TaskContainer extends Task
	{
		public var subtasks:Array;
		protected var autoStartAddedTasks:Boolean;
		
		public function TaskContainer(autoStartAddedTasks:Boolean = true, ...args)
		{
			this.autoStartAddedTasks = autoStartAddedTasks;
			this.subtasks = [];
			this.addTaskOrArray(args);
		}

		protected function cloneSubtasks():Array
		{
			var clonedTasks:Array = [];
			for each (var task:ITask in subtasks)
				clonedTasks.push(task.clone());
			return clonedTasks;
		}

		/** @inheritDoc */
		public override function clone():ITask
		{
			return new TaskContainer(autoStartAddedTasks, cloneSubtasks());
		}

		/** Add a task to the container. */
		public function add(task:ITask):void
		{
			this.subtasks.push(task);
			task.addEventListener(TaskEvent.COMPLETE, this.onSubTaskComplete);
			if (autoStartAddedTasks && this.running)
				task.start();
		}

		protected function addTaskOrArray(x:*):void
		{
			if (x is Array)
			{
				for each (var t:* in x)
					this.addTaskOrArray(t);
			}
			else
				this.add(x as ITask);
		}

		protected function disposeTask(task:ITask):void
		{
			task.removeEventListener(TaskEvent.COMPLETE, this.onSubTaskComplete);
			task.dispose();
		}
		
		/** @inheritDoc */
		public function remove(task:ITask):void
		{
			disposeTask(task);
			ArrayUtil.removeElement(subtasks, task);
		}

		protected function onSubTaskComplete(ev:TaskEvent):void
		{
			var task:ITask = ev.target as ITask;
			this.remove(task);
		}

		/**
		 * @inheritDoc
		 */
		public override function start():void
		{
			if (running) return;
			super.start();
			if (autoStartAddedTasks)
				for each (var task:ITask in subtasks.slice())
					task.start();
		}
		
		/**
		 * @inheritDoc
		 */
		public override function stop():void
		{
			if (!running) return;
			super.stop();
			if (autoStartAddedTasks)
				for each (var task:ITask in subtasks.slice())
					task.stop();
		}
		
		/** @inheritDoc */
		public override function dispose():void
		{
			for each (var task:ITask in subtasks)
				disposeTask(task);
			
			subtasks.length = 0;
			super.dispose();
		}

		/** @inheritDoc */
		public override function update(milliseconds:uint):void
		{
			super.update(updateSubtasks(milliseconds));
		}

		protected function updateSubtasks(milliseconds:uint):uint
		{
			var consumed:uint = 0;
			for each (var task:ITask in subtasks.slice())
			{
				if (task.running)
					consumed = Math.max(consumed, updateSubtask(task, milliseconds));
			}
			return running ? consumed : milliseconds;
		}
		
		public function get empty() : Boolean
		{
			return subtasks.length == 0;
		}
	}
}
